package tp.pr5.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;

import tp.pr5.Constants;
import tp.pr5.RobotEngineObserver;

public class MainWindow extends JFrame implements RobotEngineObserver
{
	private static final long serialVersionUID = 1L;
	
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItem;
	
	private InstructionPanel instructionPanel;
	private InfoPanel infoPanel;
	private NavigationPanel navigationPane;
	private RobotPanel robotPanel;
	
	private GUIController gameController;
	private Container contentPane;

	

	/**
	 * Main Constructor
	 */
	
	public MainWindow(GUIController gameController)
	{
		
		super("WALL·E The Garbage Collector");
		this.gameController = gameController;
		this.menuBar = new JMenuBar();
		this.menu = new JMenu("File");
		this.menuItem = new JMenuItem("Quit");
		this.setVisible(true);
		this.pack();
		build();
		//OBSERVERS
		gameController.registerEngineObserver(this);
		gameController.registerEngineObserver(instructionPanel);
		gameController.registerRobotObserver(infoPanel);
		gameController.registerItemContainerObserver(infoPanel);
		gameController.registerEngineObserver(infoPanel);
		gameController.registerItemContainerObserver(robotPanel);
		gameController.registerEngineObserver(robotPanel);
		gameController.registerRobotObserver(navigationPane);
	}
	
	/**
	 * Builds the user interface
	 */
	
	public void build()
	{
		this.contentPane = new Container();

		this.add(contentPane);
		contentPane.setLayout(new BorderLayout());
		
        
		this.robotPanel = new RobotPanel(); 
		
		this.navigationPane = new NavigationPanel();
		this.instructionPanel = new InstructionPanel(gameController, robotPanel);
		this.infoPanel = new InfoPanel();
		
		JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, instructionPanel, robotPanel);
		jSplitPane.setDividerLocation(245);
		
		contentPane.add(jSplitPane, BorderLayout.NORTH);
		contentPane.add(navigationPane, BorderLayout.CENTER);
		contentPane.add(infoPanel, BorderLayout.SOUTH);

		this.menuBar.add(this.menu);
		this.menu.add(this.menuItem);
		this.setJMenuBar(this.menuBar);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setMinimumSize(new Dimension(850,650));
		this.setLocationRelativeTo(null);
		
		//ACTION LISTENER FOR QUIT INSTRUCTION
		this.menuItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) 
			{
				int close = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);

				if(close == JOptionPane.YES_OPTION)
				{
					gameController.executeQuitAction();
				}
			}
		});
	}
	
	/**
	 * The robot engine informs that it has raised an error
	 * 
	 * @param msg - Error message
	 */
	public void raiseError(String msg)
	{
		JOptionPane.showMessageDialog(null, msg);
	}
	
	public void communicationHelp(String help)
	{
		//empty
	}
	
	/**
	 * The robot engine informs that the robot has shut down (because it has arrived at the spaceship or it has run out of fuel)
	 * 
	 * @param atShip - true if the robot shuts down because it has arrived at the spaceship or false if it has run out of fuel
	 */
	public void engineOff(boolean atShip)
	{
		if(atShip)
		{
			JOptionPane.showMessageDialog(null, Constants.MESSAGE_FIN_SPACESHIP);
		}
		else
		{
			JOptionPane.showMessageDialog(null, Constants.MESSAGE_DIE);
		}
		this.setEnableContainer(this.contentPane, false);
	}
	
	/**
	 * The robot engine informs that the communication is over.
	 * 
	 */
	public void communicationCompleted()
	{
		this.dispose();
	}
	
	public void robotUpdate(int fuel,int recycledMaterial)
	{
		//empty
	}
	
	public void robotSays(String message)
	{
		JOptionPane.showMessageDialog(null, message);
	}
	
	/**
	 * Disable all components inside the content pane
	 * 
	 * @param c ContentPane where the panels and or the other components are
	 * @param band false if u want to disable components
	 */
	public void setEnableContainer(Container c, boolean band)
	{
		 Component[] components = c.getComponents();

		 for(int i = 0; i < components.length; i++)
		 {            
			 components[i].setEnabled(band);
		     setEnableContainer((Container)components[i], band);
		 }      
	} 

}
